package com.socialmediaanalytics.client.interests;

import java.util.Arrays;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.socialmediaanalytics.client.GWTEntryPoint;
import com.socialmediaanalytics.client.facebook.FacebookImage;
import com.socialmediaanalytics.client.model.User;
import com.socialmediaanalytics.client.recomendations.ShowBookRecommendations;
import com.socialmediaanalytics.client.recomendations.ShowLikeRecommendations;

/** 
 * 
 * 
 * @Feature 
 */

public class ShowTopFriends extends VerticalPanel {

	public static HorizontalPanel panelFriends = new HorizontalPanel();

	public ShowTopFriends() {

		this.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		HTML title = new HTML("<b>Top Friends</b>");
		this.add(title);

		title.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				showFriends();
			}
		});

		panelFriends.clear();

		panelFriends.setSpacing(3);

		this.add(panelFriends);

	}

	public static void showFriends() {

		panelFriends.clear();

		Object[] values = GWTEntryPoint.facebookCommonFriends.values()
				.toArray();

		Arrays.sort(values);

		int last5 = 0;
		if (values.length > 5) {

			last5 = Integer.parseInt(values[values.length - 5].toString());
		} else {

			if (values.length > 0) {

				last5 = Integer.parseInt(values[0].toString());
			}
		}

		int i = 0;
		for (String friendID : GWTEntryPoint.facebookCommonFriends.keySet()) {

			if (i < 5
					&& GWTEntryPoint.facebookCommonFriends.get(friendID) >= last5) {

				// if (GWTEntryPoint.facebookCommonFriends.get(friendID) > 1) {

				User friend = GWTEntryPoint.facebookFriends.get(friendID);

				String image = FacebookImage.getString(friendID,
						friend.getFacebookName());

				HTML interests = new HTML("<br><br>" + image + "<br>"
						+ GWTEntryPoint.facebookCommonFriends.get(friendID));

				panelFriends.add(interests);

				ShowLikeRecommendations.getRecommendedLikes(friendID);

				ShowBookRecommendations.getRecommendedBooks(friendID);

				i++;
				// }
			}
		}
	}

}
